// 某个模块的数据请求（增删改查！）

import $axios from "@/common/http";  // 封装的axios
import axios from "axios"   // 原生的axios
import { Message } from "element-ui"  // 提示框


export async function getCategory(id) {
    // 读列表   读某一个
    let res;
    if (id) {
        res = await $axios.get("/cateinfo", { params: { id } });
    } else {
        res = await $axios.get("/catelist", { params:{ istree: 1 }});  
    }
    return res.list;
}

// 读取某个一级分类下的二级分类数据
export async function getChildCategory(pid){
    let res = await $axios.get("/catelist", { params: { pid } });
    return res.list;
    
}

export async function addCategory(data) {
    let res = await $axios.post("/cateadd", data);
    Message.success(res.msg)
    return true;
}

export async function editCategory(data) {
    let res = await $axios.post("/cateedit", data);
    Message.success(res.msg)
    return true;
}

export async function delCategory(id) {
    if (!id) {
        Message.warning("请传入删除id");
        return;
    }
    let res = await $axios.post("/catedelete", { id })
    if (res) {
        Message.success(res.msg)
        return res.list;
    }
}